package com.sh.data.engine.application.service.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.data.engine.application.model.request.api.DataApiAuthApproveOrderRequest;
import com.sh.data.engine.application.model.request.api.DataApiAuthApproveRequest;
import com.sh.data.engine.common.enumDefinition.DataApiApproveStatusEnum;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.api.model.domain.DataApiAuthApproveDomain;
import com.sh.data.engine.domain.api.model.param.DataApiAuthApproveOrderParam;
import com.sh.data.engine.domain.api.model.param.DataApiAuthApproveParam;
import com.sh.data.engine.domain.api.service.DataApiAuthService;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.base.model.UserContext;
import com.sh.data.engine.domain.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: mengzheng.mhc
 * @date: 2024/8/16 15:51
 */
@Slf4j
@Service
public class DataApiAuthApproveApplicationService {

    @Autowired
    private DataApiAuthService dataApiAuthService;

    public PageResult<DataApiAuthApproveDomain> getApiAuthApprovePage(DataApiAuthApproveRequest dataApiAuthApproveRequest) {
        Long projectId = ServletUtils.getProjectId();
        DataApiAuthApproveParam dataApiAuthApproveParam =
            ConvertUtil.copyProperties(dataApiAuthApproveRequest, DataApiAuthApproveParam.class);
        dataApiAuthApproveParam.setProjectId(projectId);
        IPage<DataApiAuthApproveDomain> r1 =
            dataApiAuthService.getApiAuthApprovePage(dataApiAuthApproveParam);

        return PageResult.copyPropertiesAndConvert(r1, DataApiAuthApproveDomain.class);
    }

    public boolean approveUserApiAuthById(DataApiAuthApproveOrderRequest dataApiAuthApproveOrderRequest) {
        DataApiAuthApproveOrderParam dataApiAuthApproveOrderParam =
            ConvertUtil.copyProperties(
                dataApiAuthApproveOrderRequest, DataApiAuthApproveOrderParam.class);
        dataApiAuthApproveOrderParam.setProjectId(ServletUtils.getProjectId());
        dataApiAuthApproveOrderParam.setUserId(UserContext.getUserId());

        return dataApiAuthService.approveUserApiAuthById(dataApiAuthApproveOrderParam);
    }

    public boolean cancelUserApiAuthById(Long id) {
        return dataApiAuthService.cancelUserApiAuthById(
            id, DataApiApproveStatusEnum.CANCELD.getApproveStatus());
    }
}
